package modelo.bd.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import modelo.bd.exception.DAOException;
import modelo.dominio.Cliente;

public class ClienteDAO extends AbstractDAO<Cliente> {

    @Override
    public void add(Cliente entity) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void deleteWhere(String condition) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void update(Cliente entity) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected ArrayList<Cliente> select() throws DAOException {
        try {
            con.crear();
            ResultSet resultado = con.ejecutarConsulta(getQuery());
            ArrayList<Cliente> clientes = new ArrayList();
            Cliente cliente;
            while (resultado.next()) {
                cliente = new Cliente(resultado.getInt("numero"), resultado.getString("razon_social"));
                clientes.add(cliente);
            }
            resultado.close();
            con.cerrar();
            return clientes;
        } catch (ClassNotFoundException ex) {
            throw new DAOException("Error al crear la conexion " + ex.getMessage());
        } catch (SQLException ex) {
            throw new DAOException("Error en DAO Clientes " + ex.getMessage());
        } catch (Exception ex) {
            throw new DAOException("Error en el archivo de propiedades " + ex.getMessage());
        }
    }

    @Override
    public ArrayList<Cliente> selectAll() throws DAOException {
        this.setQuery("SELECT * FROM scc_clientes");
        return select();
    }

    @Override
    public ArrayList<Cliente> selectWhere(String condition) throws DAOException {

        this.setQuery("SELECT clnt_numero as numero, clnt_numero ||' - '|| substr(clnt_razon_social, 1, 35) as razon_social "
                + "FROM scc_clientes "
                + "WHERE " + condition);
        return select();
    }

    public ArrayList<Cliente> selectEspe(String condition) throws DAOException {
        this.setQuery("SELECT clnt_numero as numero, clnt_numero ||' - '|| substr(clnt_razon_social, 1, 35) as razon_social "
                + "FROM   scc_usuarios_perfil, scc_clientes_usuario, scc_clientes "
                + "WHERE  uspe_clave      = '" + condition + "' "
                + "AND    uspe_norm_rest  = 'R' "
                + "AND    clus_uspe_clave = uspe_clave "
                + "AND    clnt_numero     = clus_clnt_numero "
                + "UNION "
                + "SELECT clnt_numero, clnt_numero ||' - '|| substr(clnt_razon_social, 1, 35) clnt_razon_social "
                + "FROM   scc_usuarios_perfil, scc_clientes "
                + "WHERE  uspe_clave     = '" + condition + "' "
                + "AND    uspe_norm_rest = 'N' "
                + "order by 1");
        return select();
    }

    public ArrayList<Cliente> selectEspe2(String condition) throws DAOException {
        String user = condition;
//        this.setQuery("SELECT clnt_numero,\n"
//                + "         clnt_numero || ' - ' || SUBSTR (clnt_razon_social, 1, 30) clnt_razon_social\n"
//                + "    FROM scc_clientes_usuario, scc_clientes\n"
//                + "   WHERE     clus_uspe_clave = UPPER ('"+condition+"')\n"
//                + "         AND clnt_numero = clus_clnt_numero\n"
//                + "         AND clnt_stcl_clave = 'ACTI'\n"
//                + "ORDER BY 2");
        this.setQuery("SELECT clnt_numero, clnt_numero ||' - '|| SUBSTR(clnt_razon_social,1,30) clnt_razon_social "
                + "FROM  scc_clientes "
                + "WHERE clnt_stcl_clave = 'ACTI' "
                + "ORDER BY clnt_numero");
        return select();
    }
}
